import Vue from 'vue'
import VueRouter from 'vue-router'
import UserLogin from '@/components/UserLogin.vue'
import '../assets/css/global.css'
import Home from '../components/UserHome.vue'
import Welcome from '../components/UserWelcome'
import Users from '@/components/user/UsersList'
import Rights from '@/components/power/UserRights'
import Roles from '@/components/power/UserRoles'
import Cate from '@/components/goods/UserCate'
import Params from '@/components/goods/UserParams'
import GoodsList from '@/components/goods/UserList'
import Add from '@/components/goods/UserAdd'
import Report from '@/components/report/UserReport'

Vue.use(VueRouter)

const routes = [
  {
    path: '/', redirect: '/login'
  },
  {
    path: '/login',
    component: UserLogin
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/categories', component: Cate },
      { path: '/params', component: Params },
      { path: '/goods', component: GoodsList },
      { path: '/goods/add', component: Add },
      { path: '/reports', component: Report }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
